package com.zb.Dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

import com.zb.Conn.Conn;
import com.zb.Model.*;

public class BookDao {
	
	Connection conn=null;
	PreparedStatement pst=null;
	ResultSet rst=null;
	
	public boolean addBook(BookInfo book)
	{
		conn=Conn.getConn();
		String sql="insert into booksInfo(bookCode,bookName,bookPrice,bookNum,bookType,bookPress) values(?,?,?,?,?,?)";
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1,book.getBookCode());
			pst.setString(2,book.getBookName());
			pst.setBigDecimal(3,book.getBookPrice());
			pst.setInt(4, book.getBookNum());
			pst.setInt(5, book.getBookType());
			pst.setString(6,book.getBookPress());
			int rs=pst.executeUpdate();
			if(rs>0) return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean addBookType(String bookType)
	{
		conn=Conn.getConn();
		String sql="insert into booktype (bookType) values(?)";
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1,bookType);
			int rs=pst.executeUpdate();
			if(rs>0) return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public List<BookInfo> showBook()
	{
		List<BookInfo> res=new ArrayList<BookInfo>();
		conn=Conn.getConn();
		String sql="select * from booksinfo";
		try {
			pst=conn.prepareStatement(sql);
			rst=pst.executeQuery();
			while(rst.next())
			{
				BookInfo curBook=new BookInfo();
				curBook.setBookId(rst.getInt("bookId"));
				curBook.setBookCode(rst.getString("bookCode"));
				curBook.setBookName(rst.getString("bookName"));
				curBook.setBookNum(rst.getInt("bookNum"));
				curBook.setBookPress(rst.getString("bookPress"));
				curBook.setBookPrice(rst.getBigDecimal("bookPrice"));
				curBook.setBookType(rst.getInt("bookType"));
				res.add(curBook);
			}
			return res;
		} catch (SQLException e) {
			res.clear();
			e.printStackTrace();
		}
		return res;
	}
	
	public BookInfo getBookByBookId(int bookId)
	{
		BookInfo book=new BookInfo();
		conn=Conn.getConn();
		String sql="select * from booksInfo where bookId=?";
		try {
			pst=conn.prepareStatement(sql);
			pst.setInt(1,bookId);
			rst=pst.executeQuery();
			if(rst.next())
			{
				book.setBookId(rst.getInt("bookId"));
				book.setBookCode(rst.getString("bookCode"));
				book.setBookName(rst.getString("bookName"));
				book.setBookNum(rst.getInt("bookNum"));
				book.setBookPress(rst.getString("bookPress"));
				book.setBookPrice(rst.getBigDecimal("bookPrice"));
				book.setBookType(rst.getInt("bookType"));
				return book;
			}
			else return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean modifyBook(BookInfo bookInfo)
	{
		conn=Conn.getConn();
		String sql="update booksinfo set bookName=?,bookCode=?,bookPrice=?,bookNum=?,bookType=?,bookPress=? where bookId=?";
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1,bookInfo.getBookName());
			pst.setString(2,bookInfo.getBookCode());
			pst.setBigDecimal(3, bookInfo.getBookPrice());
			pst.setInt(4, bookInfo.getBookNum());
			pst.setInt(5, bookInfo.getBookType());
			pst.setString(6, bookInfo.getBookPress());
			pst.setInt(7, bookInfo.getBookId());
			int rs=pst.executeUpdate();
			if(rs>0) return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public BookInfo getBookByBookcode(String bookCode)
	{
		BookInfo book=new BookInfo();
		conn=Conn.getConn();
		String sql="select * from booksInfo where bookCode=?";
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1,bookCode);
			rst=pst.executeQuery();
			if(rst.next())
			{
				book.setBookId(rst.getInt("bookId"));
				book.setBookCode(rst.getString("bookCode"));
				book.setBookName(rst.getString("bookName"));
				book.setBookNum(rst.getInt("bookNum"));
				book.setBookPress(rst.getString("bookPress"));
				book.setBookPrice(rst.getBigDecimal("bookPrice"));
				book.setBookType(rst.getInt("bookType"));
				return book;
			}
			else return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public Map<Integer,String> showBookType()
	{
		Map<Integer,String> res=new HashMap<Integer,String>();
		conn=Conn.getConn();
		String sql="select * from booktype";
		try {
			pst=conn.prepareStatement(sql);
			rst=pst.executeQuery();
			while(rst.next())
			{
				res.put(rst.getInt("id"),rst.getString("bookType"));
			}
			return res;
		} catch (SQLException e) {
			e.printStackTrace();
			res.clear();
		}
		return res;
	}
	
	public String getBookType(int id)
	{
		String res=null;
		conn=Conn.getConn();
		String sql="select bookPress from booktype where id=?";
		try {
			pst=conn.prepareStatement(sql);
			pst.setInt(1,id);
			rst=pst.executeQuery();
			if(rst.next())
			{
				res=rst.getString("bookPress");
			}
			return res;
		} catch (SQLException e) {
			e.printStackTrace();
			res=null;
		}
		return res;
	}
	
}
